package libraryClass;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

import userclass.UserUnit;

public class Library  implements Serializable{
	public enum LendOrBack{
		LEND,BACK
	}
	private static Map<Book,Integer> books=new TreeMap<Book,Integer>();
	public Map<Book,Integer> books1=new TreeMap<Book,Integer>();

	//	static{
//		initializeBookStore();
//	}
	public static void initializeBookStore(){
		Book book1 = new Book("红与黑",111,"文学类");
		Book book2 = new Book("平凡的世界",112,"文学类");
		Book book3 = new Book("双城记",113,"文学类");
		Book book4 = new Book("资本论",114,"经济类");
		Book book5 = new Book("孙子兵法",115,"军事类");
		Book book6 = new Book("人间词话",116,"艺术类");
		Book book7 = new Book("诗经",117,"艺术类");
		Book book8 = new Book("楚辞",118,"艺术类");
		Book book9 = new Book("战国策",119,"历史类");
		Book book10 = new Book("国家地理",120,"地理类");
		books.put(book1, 30);
		books.put(book2, 50);
		books.put(book3, 40);
		books.put(book4, 10);
		books.put(book5, 20);
		books.put(book6, 15);
		books.put(book7, 35);
		books.put(book8, 40);
		books.put(book9, 5);
		books.put(book10, 10);


	}
	public static Map<Book, Integer> getBooks() {
		return books;
	}

	public static Book peekBook(long ID) {
		Iterator<Map.Entry<Book, Integer>> m = books.entrySet().iterator();
		while(m.hasNext()){
			Map.Entry<Book, Integer> e = m.next();
			if(e.getKey().getID()==ID&&e.getValue()>0)
				return e.getKey();
		}
		return null;
	}
	public static ArrayList<Book> peekBooks(String name) {
		ArrayList<Book> bookList=new ArrayList<Book> ();
		Iterator<Map.Entry<Book, Integer>> m = books.entrySet().iterator();
		while(m.hasNext()){
			Map.Entry<Book, Integer> e = m.next();
			if(e.getKey().getName()==name&&e.getValue()>0)
				bookList.add(e.getKey());

		}
		return bookList;
	}

	public static void add(Book book,int num){
		if(books.get(book)==null){
			books.put(book, num);
		}
		else{
			books.put(book, books.get(book)+num);
		}

	}
	public static void remove(Book book){
		books.remove(book);
	}

	public static void lendOrBack(Book book,LendOrBack action){
		switch(action) {
			case LEND:books.put(book, books.get(book)-1);	break;
			case BACK: books.put(book, books.get(book)+1);	break;
		}
//		books.put(book, books.get(book)-1):books.put(book, books.get(book)+1);
	}


	public static void display(PrintWriter out){
		Iterator<Map.Entry<Book, Integer>> m = books.entrySet().iterator();

		out.println("     书名   \t\tID\t\t类别\t\t数量");
		while(m.hasNext()){
			Map.Entry<Book, Integer> e = m.next();
			if(e.getValue()>0)
				out.println(e.getKey().toString()+"\t\t"+e.getValue());
		}
		out.println();

	}

	public void save()throws 	IOException{

		try(ObjectOutputStream output= new ObjectOutputStream(new FileOutputStream("Library.txt"))){

			books1=books;
			output.writeObject(this);

		}
	}


	public static void load(  String fileName ) throws FileNotFoundException, IOException, ClassNotFoundException {

		try(ObjectInputStream input =  new  ObjectInputStream(new  FileInputStream("Library.txt") )){

			Library l=(Library) input.readObject();
			books=l.books;

		}
	}


}

